package com.hnu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hnu.common.Constants;
import com.hnu.dto.Result;
import com.hnu.entity.Admin;
import com.hnu.entity.Product;
import com.hnu.service.AdminService;
import com.hnu.service.ProductService;
import com.hnu.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Date;

/**
 * @Author hj
 * @Date 2023/12/20 8:49
 * @PackageName:com.hnu.controller
 * @ClassName: AdminController
 * @Description: create a class
 * @Version 1.0
 */
@CrossOrigin //服务器端解决跨域
@RequestMapping("/product")
@RestController
public class ProductController {

    @Autowired
    private ProductService productService;

    /**
     * 分页查询商品
     *
     * @param pageNum  pageNum
     * @param pageSize pageSize
     * @param product  product
     * @return Result
     */
    @PostMapping("/findAll/{pageNum}/{pageSize}")
    private Result page(@PathVariable int pageNum,
                        @PathVariable int pageSize,
                        @RequestBody Product product) {
        QueryWrapper<Product> qw = new QueryWrapper<>();
        if (product.getCategoryid() != null) {
            qw.eq("categoryid", product.getCategoryid());
        }
        if (StringUtils.isNotBlank(product.getProductName())) {
            qw.like("product_name", product.getProductName());
        }
        IPage<Product> page = productService.page(new Page<>(pageNum, pageSize), qw);
        return Result.ok(Constants.OK).data(page);
    }

    /**
     * 根据ID查询商品
     *
     * @param id
     * @return Result
     */
    @PostMapping("/query/{id}")
    private Result findById(@PathVariable long id) {
        Product product = productService.getById(id);
        return Result.ok(Constants.OK).data(product);
    }

    @PostMapping("/upload")
    public Result upload(@RequestBody MultipartFile file) {
        if (file.isEmpty()) {
            return Result.error(Constants.UPLOAD_ERROR);
        }
        //获取原始文件名称
        String originalFilename = file.getOriginalFilename();
        //生成随机名称
        String fileName = System.currentTimeMillis() + "." + originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
        //设置保存地址
        String path = System.getProperty("user.dir") + "/src/main/resources/static/images/";
        String filePath = path + fileName;
        File dest = new File(filePath);

        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdir();
        }
        try {
            file.transferTo(dest);
            return Result.ok(Constants.UPLOAD_SUCCESS).data(fileName);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.error(Constants.UPLOAD_ERROR);
        }
    }

    @PostMapping("/update")
    public Result update(@RequestBody Product product) {
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("id", product.getId());
        product.setProductUpdateTime(new Date());
        productService.update(product,qw);
        return Result.ok(Constants.UPDATE_OK);
    }

    @PostMapping("/save")
    public Result save(@RequestBody Product product){
        product.setProductUpdateTime(new Date());
        product.setProductCreateTime(new Date());
        productService.save(product);
        return Result.ok(Constants.ADD_SUCCESS);
    }

    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable long id){
        productService.removeById(id);
        return Result.ok(Constants.DELETE_OK);
    }
}
